/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.editor;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class AppWindow extends JPanel {
	
	private final JPanel top 		= new JPanel();
	private final JPanel bottom 	= new JPanel();
	private final JPanel left		= new JPanel();
	private final JPanel right		= new JPanel();
	
	private final MainPanel content = new MainPanel();
	
	private JMenuBar mainMenu = new JMenuBar();
	
	public AppWindow() {
		this.setLayout(new BorderLayout());
		
		top.setLayout(new GridLayout());
		
		JMenu file = mainMenu.add(new JMenu("File"));
		file.setMnemonic('F');
		
		createMenuItem(file, KeyEvent.VK_O, "ctrl O", new AbstractAction("Open File"){
			public void actionPerformed(ActionEvent e) {
				content.openFile();
			}
		});
		
		createMenuItem(file, KeyEvent.VK_S, "ctrl S", new AbstractAction("Save File"){
			public void actionPerformed(ActionEvent e) {
				content.saveFile();
			}		
		});
		
		createMenuItem(file, KeyEvent.VK_A, "ctrl alt S", new AbstractAction("Save File As..."){
			public void actionPerformed(ActionEvent e) {
				content.saveFileAs();
			}
		});
		
		file.addSeparator();
		
		createMenuItem(file, KeyEvent.VK_X, "alt X", new AbstractAction("Exit") {
			public void actionPerformed(ActionEvent e) {
				content.exit();
			}
		});
		
		JMenu edit = mainMenu.add(new JMenu("Edit"));
		edit.setMnemonic('E');
		
		createMenuItem(edit, KeyEvent.VK_1, "alt 1", new AbstractAction("Show Data"){
			public void actionPerformed(ActionEvent e) {
				content.focusData();
			}		
		});
		
		
		createMenuItem(edit, KeyEvent.VK_2, "alt 2", new AbstractAction("Show Metadata"){
			public void actionPerformed(ActionEvent e) {
				content.focusMetadata();
			}
		});
		
		createMenuItem(edit, KeyEvent.VK_3, "alt 3", new AbstractAction("Show Raw Data"){
			public void actionPerformed(ActionEvent e) {
				content.focusHexTable();
			}
		});
		
		createMenuItem(edit, KeyEvent.VK_3, "alt 4", new AbstractAction("Show Page Content"){
			public void actionPerformed(ActionEvent e) {
				content.focusPageContent();
			}
		});
		
		edit.addSeparator();
		
		createMenuItem(edit, KeyEvent.VK_F, "ctrl F", new AbstractAction("Find Model"){
			public void actionPerformed(ActionEvent e) {
				content.findModel();
			}
		});
		
		JMenu help = mainMenu.add(new JMenu("Help"));
		help.setMnemonic('H');
		
		help.add(new AbstractAction("About"){
			public void actionPerformed(ActionEvent e) {
				
			}			
		});
		
		top.add(mainMenu);
		
		this.add(top, 		BorderLayout.NORTH);
		
		this.add(content, BorderLayout.CENTER);
	}
	
	public void createMenuItem(JMenu menu, int menemonic, String accelerator, Action action) {
		JMenuItem item = menu.add(new JMenuItem(action));
		
		item.setMnemonic(menemonic);
		item.setAccelerator(KeyStroke.getKeyStroke(accelerator));
	}

	public JPanel getTop() {
		return top;
	}

	public JPanel getBottom() {
		return bottom;
	}

	public JPanel getLeft() {
		return left;
	}

	public JPanel getRight() {
		return right;
	}

	public MainPanel getContent() {
		return content;
	}
	
	public void focus() {
		content.focusData();
	}
	
	public void openFile(String file) {
		content.openFile(file);
	}
	
	public AppContext getAppContext() {
		return content.getAppContext();
	}

	public void setAppContext(AppContext appContext) {
		this.content.setAppContext(appContext);
	}
}
